package com.yunkaweilai.android.activity.operation;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.CaptureActivity;
import com.yunkaweilai.android.activity.MainActivity;
import com.yunkaweilai.android.activity.member.AddMemberInfoActivity;
import com.yunkaweilai.android.activity.member.MemberInfoListActivity;
import com.yunkaweilai.android.activity.member.MemberRechargeActivity;
import com.yunkaweilai.android.activity.operation.MemberLock.LockActivity;
import com.yunkaweilai.android.activity.operation.consumption.jici.JiciListActivity;
import com.yunkaweilai.android.activity.operation.integral.IntegralExchangeActivity;
import com.yunkaweilai.android.activity.operation.integral.IntegralSettingActivity;
import com.yunkaweilai.android.activity.operation.recharge.JiciRechargeActivity;
import com.yunkaweilai.android.activity.operation.score.ScoreChangeActivity2;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentConsume;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentIntegral;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentJici;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentLock;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentMember;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentRecharge;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentRechargeJici;
import com.yunkaweilai.android.fragment.member.CommonMemberFragmentScore;
import com.yunkaweilai.android.model.jici.CommonMemberModel;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.model.member.MemberLevelModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.InputMethodUtils;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;
import com.zhy.m.permission.MPermissions;
import com.zhy.m.permission.PermissionDenied;
import com.zhy.m.permission.PermissionGrant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 通用会员查找
 * Created by Administrator on 2019/9/16.
 */

public class CommonMemberActivity extends BaseActivity {

    @BindView(R.id.id_listView_search)
    ListView listViewSearch;
    @BindView(R.id.line_search)
    LinearLayout Linesearch;
    @BindView(R.id.id_edt_search)
    EditText idEdtSearch;
    @BindView(R.id.id_img_clean)
    ImageView idImgClean;
    @BindView(R.id.id_img_qr_code)
    ImageView idImgQrCode;
    @BindView(R.id.config_hidden)
    TextView mTextViewhidden;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView multipleStatusView;
    @BindView(R.id.id_tv_all_member)
    TextView idTvAllMember;
    @BindView(R.id.id_tabLayout)
    TabLayout idTabLayout;
    @BindView(R.id.id_viewPager)
    ViewPager idViewPager;
    private int mpage = 1;
    private ArrayList<MemberHeadModel> memberList = new ArrayList<>();
    private ArrayList<MemberLevelModel.DataBean.ListBean> list = new ArrayList<>();
    private CommonAdapter<MemberHeadModel> adapter;
    private String keywords = "";
    public static final String SEARCH_MEMBER_FLAG = "SEARCH_MEMBER_FLAG";//是否选中会员
    public static final String SEARCH_MEMBER_MODEL = "SEARCH_MEMBER_MODEL";
    public static final String SEARCH_MEMBER_BUNDLE = "SEARCH_MEMBER_BUNDLE";
    public static final String FROM = "FROM";

    //todo:k 新建step1
    public static String TYPE_CONSUME_MEMBER = "TYPE_CONSUME_MEMBER";//消费选人
    public static String TYPE_JICI_MEMBER = "TYPE_JICI_MEMBER";//计次消费
    public static String TYPE_MEMBER = "TYPE_MEMBER";   //会员管理 用member_list
    public static String TYPE_RECHARGE_MEMBER = "TYPE_RECHARGE_MEMBER";//余额充值
    public static String TYPE_RECHARGE_MEMBER_JICI = "TYPE_RECHARGE_MEMBER_JICI";//计次充值
    public static String TYPE_INTEGRAL_MEMBER = "TYPE_INTEGRAL_MEMBER";//积分兑换
    public static String TYPE_SCORE_MEMBER = "TYPE_SCORE_MEMBER";//积分调整
    public static String TYPE_MESSAGE_MEMBER = "TYPE_MESSAGE_MEMBER";//短信群发选人
    public static String TYPE_MEMBER_LOCK = "TYPE_MEMBER_LOCK";//会员解锁选人

    private String type = TYPE_CONSUME_MEMBER;//设置收款界面为特殊化，因为是startActivityForResult进来的

    //传入是从哪个activity来的参数，决定对应的跳转和界面
    public static void actionstart(Context context, String from) {
        Intent intent = new Intent(context, CommonMemberActivity.class);
        intent.putExtra(FROM, from);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_member);
        ButterKnife.bind(this);
        mTextViewhidden.requestFocus();//获取焦点，防止弹出软键盘
        if (!(getIntent().getStringExtra(FROM) == null)) {
            type = getIntent().getStringExtra(FROM);
        }
        //根据不同的type设置不同的title
        inittitle();

        initadapterForSearch();
        initClick();
        initView();
    }

    //todo:k 新建step2
    private void inittitle() {
        if (type.equals(TYPE_INTEGRAL_MEMBER)) {
            new TitleBuilder(mAc)
                    .setTitleText("搜索会员")
                    .setLeftImage(R.mipmap.ic_go_back)
                    .setLeftOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            InputMethodUtils.closeSoftKeyboard(mAc);
                            intent2Activity(MainActivity.class);
                            finish();
                        }
                    })
                    .setRightText("礼品设置")
                    .setRightOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            InputMethodUtils.closeSoftKeyboard(mAc);
                            intent2Activity(IntegralSettingActivity.class);
                        }
                    });
        } else if (type.equals(TYPE_MEMBER)) {//会员管理
//            idTvAllMember.setVisibility(View.VISIBLE);//显示会员数量 (已改为在fragment里面显示)
            if (BaseApplication.appRuleModel.isIs_super() || BaseApplication.appRuleModel.getApp_rule().get("18")) {//有权限
                new TitleBuilder(mAc)
                        .setTitleText("会员管理")
                        .setLeftImage(R.mipmap.ic_go_back)
                        .setLeftOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                InputMethodUtils.closeSoftKeyboard(mAc);
                                finish();
                            }
                        })
                        .setRightText("新增")
                        .setRightOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (BaseApplication.appRuleModel.isIs_super() || BaseApplication.appRuleModel.getApp_rule().get("18")) {
                                    intent2Activity(AddMemberInfoActivity.class);
                                }
                            }
                        });
            } else {
                new TitleBuilder(mAc)
                        .setTitleText("会员管理")
                        .setLeftImage(R.mipmap.ic_go_back)
                        .setLeftOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                InputMethodUtils.closeSoftKeyboard(mAc);
                                finish();
                            }
                        });
            }
        } else if (type.equals(TYPE_CONSUME_MEMBER)) {
            new TitleBuilder(mAc)
                    .setLeftImage(R.mipmap.ic_go_back)
//                .setTitleText(getResources().getString(R.string.tab_main_2))
                    .setTitleText("搜索会员")
                    .setLeftOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra(SEARCH_MEMBER_FLAG, false);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(SEARCH_MEMBER_MODEL, null);
                            intent.putExtra(SEARCH_MEMBER_BUNDLE, bundle);
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    });
        } else {
            new TitleBuilder(mAc)
                    .setTitleText("搜索会员")
                    .setLeftImage(R.mipmap.ic_go_back)
                    .setLeftOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            InputMethodUtils.closeSoftKeyboard(mAc);
                            finish();
                        }
                    });
        }
    }

    private void initadapterForSearch() {
        //积分调整的item有差别
        if (type.equals(TYPE_SCORE_MEMBER)) {
            adapter = new CommonAdapter<MemberHeadModel>(mAc, R.layout.item_list_member, memberList) {
                @Override
                protected void convert(ViewHolder viewHolder, MemberHeadModel item, int position) {
                    ImageView idImgHead = viewHolder.getView(R.id.id_img_head);
                    Utils.MemberHeadImg(mAc, item.getMember_avatar(), item.getMember_sex(), idImgHead);
                    TextView idTvName = viewHolder.getView(R.id.id_tv_name);
                    //设置名字、手机和状态
                    if (item.getStatusStr().equals("过期")) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已过期" + "</font>");
                    } else if ("2".equals(item.getMl_status())) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已锁定" + "</font>");
                    } else if ("3".equals(item.getMl_status())) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已挂失" + "</font>");
                    } else {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName);
                    }
                    viewHolder.setText(R.id.id_tv_level, "等级：" + item.getLevel_name() + "     卡号：" + item.getCard_no());
                    TextView textView1 = viewHolder.getView(R.id.id_tv_discount);
                    textView1.setText(Html.fromHtml("折扣：" + item.getDiscount() / 10 + "折&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;余额：" + item.getMember_price() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;积分：" + "<font color='#F53435'>" + item.getMember_points() + "</font>"));
                }
            };
        }
        //会员管理这里用的接口不一样，判断过期的值不一样
        else if (type.equals(TYPE_MEMBER) || type.equals(TYPE_MEMBER_LOCK)) {
            adapter = new CommonAdapter<MemberHeadModel>(mAc, R.layout.item_list_member, memberList) {
                @Override
                protected void convert(ViewHolder viewHolder, MemberHeadModel item, int position) {
                    ImageView idImgHead = viewHolder.getView(R.id.id_img_head);
                    Utils.MemberHeadImg(mAc, item.getMember_avatar(), item.getMember_sex(), idImgHead);
                    TextView idTvName = viewHolder.getView(R.id.id_tv_name);
                    //设置名字、手机和状态
                    if (item.getStatus_info().is_valid()) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已过期" + "</font>");
                    } else if ("2".equals(item.getMl_status())) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已锁定" + "</font>");
                    } else if ("3".equals(item.getMl_status())) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已挂失" + "</font>");
                    } else {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName);
                    }
                    viewHolder.setText(R.id.id_tv_level, "等级：" + item.getLevel_name() + "     卡号：" + item.getCard_no());
                    viewHolder.setText(R.id.id_tv_discount, "折扣：" + item.getDiscount() / 10 + "折   积分：" + item.getMember_points() + "   余额：" + item.getMember_price());
                }
            };
        } else {
            adapter = new CommonAdapter<MemberHeadModel>(mAc, R.layout.item_list_member, memberList) {
                @Override
                protected void convert(ViewHolder viewHolder, MemberHeadModel item, int position) {
                    ImageView idImgHead = viewHolder.getView(R.id.id_img_head);
                    Utils.MemberHeadImg(mAc, item.getMember_avatar(), item.getMember_sex(), idImgHead);
                    TextView idTvName = viewHolder.getView(R.id.id_tv_name);
                    //设置名字、手机和状态
                    if (item.getStatusStr().equals("过期")) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已过期" + "</font>");
                    } else if ("2".equals(item.getMl_status())) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已锁定" + "</font>");
                    } else if ("3".equals(item.getMl_status())) {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName, "<font color='" + getResources().getColor(R.color.calu_red) + "'>" + "已挂失" + "</font>");
                    } else {
                        Utils.memberNameSet(item.getMember_name(), item.getMember_tel(), idTvName);
                    }
                    viewHolder.setText(R.id.id_tv_level, "等级：" + item.getLevel_name() + "     卡号：" + item.getCard_no());
                    viewHolder.setText(R.id.id_tv_discount, "折扣：" + item.getDiscount() / 10 + "折   积分：" + item.getMember_points() + "   余额：" + item.getMember_price());
                }
            };
        }
        listViewSearch.setAdapter(adapter);
    }

    private void initView() {
        /**
         * 获取会员等级
         */
        HttpRequest.postUrl(Api.MEMBER_LEVEL_LIST)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        dismissProgressDialog();
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            MemberLevelModel memberLevelModel = gson.fromJson(json, MemberLevelModel.class);
                            list.clear();
                            MemberLevelModel.DataBean.ListBean listBean = new MemberLevelModel.DataBean.ListBean();
                            listBean.setId("0");
                            listBean.setLevel_name("全部");
                            list.add(listBean);
                            list.addAll(memberLevelModel.getData().getList());
                            CommonMemberAdapter adapter = new CommonMemberAdapter(getSupportFragmentManager(), list);
                            idViewPager.setAdapter(adapter);
                            idTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                            idTabLayout.setupWithViewPager(idViewPager);
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        dismissProgressDialog();
                    }
                });
    }

    //todo:k 新建step3
    private void inithttpsearch(String keyword) {
        if (type.equals(TYPE_JICI_MEMBER)) {
            HttpRequest.postUrl(Api.MEMBER_COMMON_LIST)
                    .addParams("ShowPage", "1")
                    .addParams("PageSize", "10")
                    .addParams("keyword", keyword)
                    .addParams("CurrPage", mpage + "")
                    .addParams("ConsumeType", "Jici")//计次消费要传这个参数
                    .execute(new PostProcess.StringCallBack() {
                        @Override
                        public void onResponse(String json) {
                            boolean checkcode = Utils.checkCode(mAc, json);
                            if (checkcode) {
                                Gson gson = new Gson();
                                CommonMemberModel CommonMemberModel = gson.fromJson(json, CommonMemberModel.class);
                                memberList.clear();
                                memberList.addAll(CommonMemberModel.getData().getList());
                                if (memberList.size() > 0) {
                                    hideSearch(true);
                                } else {
                                    showToast("该会员不存在");
                                }
                            } else {
                                showToast("服务器出错");
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                            showToast(e.getMessage());
                        }
                    });
        } else if (type.equals(TYPE_MEMBER)) {
            HttpRequest.postUrl(Api.MEMBER_LIST)
                    .addParams("ShowPage", "1")
                    .addParams("PageSize", "10")
                    .addParams("CurrPage", mpage + "")
                    .addParams("keyword", keyword)
                    .execute(new PostProcess.StringCallBack() {
                        @Override
                        public void onResponse(String json) {
                            boolean checkcode = Utils.checkCode(mAc, json);
                            if (checkcode) {
                                Gson gson = new Gson();
                                CommonMemberModel CommonMemberModel = gson.fromJson(json, CommonMemberModel.class);
                                memberList.clear();
                                memberList.addAll(CommonMemberModel.getData().getList());
                                if (memberList.size() > 0) {
                                    hideSearch(true);
                                } else {
                                    showToast("该会员不存在");
                                }
                            } else {
                                showToast("服务器出错");
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                            showToast(e.getMessage());
                        }
                    });
        } else if (type.equals(TYPE_MEMBER_LOCK)) {
            HttpRequest.postUrl(Api.MEMBER_LOCK)
                    .addParams("ShowPage", "1")
                    .addParams("PageSize", "10")
                    .addParams("keyword", keyword)
                    .addParams("CurrPage", mpage + "")
                    .execute(new PostProcess.StringCallBack() {
                        @Override
                        public void onResponse(String json) {
                            boolean checkcode = Utils.checkCode(mAc, json);
                            if (checkcode) {
                                Gson gson = new Gson();
                                CommonMemberModel CommonMemberModel = gson.fromJson(json, CommonMemberModel.class);
                                memberList.clear();
                                memberList.addAll(CommonMemberModel.getData().getList());
                                if (memberList.size() > 0) {
                                    hideSearch(true);
                                } else {
                                    showToast("该会员不存在");
                                }
                            } else {
                                showToast("服务器出错");
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                            showToast(e.getMessage());
                        }
                    });
        } else {
            HttpRequest.postUrl(Api.MEMBER_COMMON_LIST)
                    .addParams("ShowPage", "1")
                    .addParams("PageSize", "10")
                    .addParams("keyword", keyword)
                    .addParams("CurrPage", mpage + "")
                    .execute(new PostProcess.StringCallBack() {
                        @Override
                        public void onResponse(String json) {
                            boolean checkcode = Utils.checkCode(mAc, json);
                            if (checkcode) {
                                Gson gson = new Gson();
                                CommonMemberModel CommonMemberModel = gson.fromJson(json, CommonMemberModel.class);
                                memberList.clear();
                                memberList.addAll(CommonMemberModel.getData().getList());
                                if (memberList.size() > 0) {
                                    hideSearch(true);
                                } else {
                                    showToast("该会员不存在");
                                }
                            } else {
                                showToast("服务器出错");
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                            showToast(e.getMessage());
                        }
                    });
        }
    }

    private void hideSearch(boolean hideMutiple) {
        if (hideMutiple) {
            multipleStatusView.setVisibility(View.GONE);
            Linesearch.setVisibility(View.VISIBLE);
        } else {
            multipleStatusView.setVisibility(View.VISIBLE);
            Linesearch.setVisibility(View.GONE);
        }
    }

    private void initClick() {
        //搜索框
        idEdtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null && idEdtSearch.length() > 0) {
                    //点击搜索要做的操作
                    keywords = idEdtSearch.getText().toString();
                    InputMethodUtils.closeSoftKeyboard(mAc);
                    inithttpsearch(keywords);
                } else {
                    hideSearch(false);
                    InputMethodUtils.closeSoftKeyboard(mAc);
                }
                return false;
            }
        });

        idEdtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s == null || s.length() == 0) {
                    idImgClean.setVisibility(View.GONE);
                    return;
                } else {
                    idImgClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //todo:k step6
        listViewSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (type.equals(TYPE_MEMBER)) {//会员管理
                    if (BaseApplication.getAppRuleModel().isIs_super() || BaseApplication.getAppRuleModel().getApp_rule().get("17")) {
                        MemberInfoListActivity.actionStart(mAc, memberList.get(position),"1");
                    }
                }
                if (type.equals(TYPE_JICI_MEMBER)) {//计次消费
                    MemberHeadModel listBean = memberList.get(position);
                    if (listBean.is_click()) {
                        JiciListActivity.actionStart(mAc, listBean.getId());
                    } else {
                        if (listBean.getMl_status().equals("2")) {
                            showToast("该会员已锁定");
                        } else if (listBean.getMl_status().equals("3")) {
                            showToast("该会员已挂失");
                        } else if (listBean.getStatusStr().equals("过期")) {
                            showToast("该会员已过期");
                        }
                    }
                }
                if (type.equals(TYPE_RECHARGE_MEMBER)) {//余额充值
                    MemberHeadModel listBean = memberList.get(position);
                    if ("2".equals(listBean.getMl_status())) {
                        showToast("会员已锁定");
                    } else if ("3".equals(listBean.getMl_status())) {
                        showToast("会员已挂失");
                    } else {
                        if ("1".equals(listBean.getIs_stored())) {
                            if (!listBean.getStatusStr().equals("过期")) {
                                MemberRechargeActivity.actionStart(mAc, listBean);
                            } else {
                                showToast("会员已过期");
                            }
                        } else {
                            showToast("该会员还未开启储值功能");
                        }
                    }
                }
                if (type.equals(TYPE_RECHARGE_MEMBER_JICI)) {//计次充值
                    MemberHeadModel listBean = memberList.get(position);
                    if (listBean.is_click()) {
                        BaseApplication.getSelectedMaps().clear();
                        JiciRechargeActivity.actionStart(mAc, listBean);
                    } else {
                        if (listBean.getMl_status().equals("2")) {
                            showToast("该会员已锁定");
                        } else if (listBean.getMl_status().equals("3")) {
                            showToast("该会员已挂失");
                        } else if (listBean.getStatusStr().equals("过期")) {
                            showToast("该会员已过期");
                        }

                    }
                }
                if (type.equals(TYPE_INTEGRAL_MEMBER)) {//积分兑换
                    MemberHeadModel listBean = memberList.get(position);
                    if (listBean.is_click()) {
                        IntegralExchangeActivity.actionStart(mAc, listBean.getId(), Integer.parseInt(listBean.getMember_points()), listBean.getCard_no());
                    } else {
                        if (listBean.getMl_status().equals("2")) {
                            showToast("该会员已锁定");
                        } else if (listBean.getMl_status().equals("3")) {
                            showToast("该会员已挂失");
                        } else if (listBean.getStatusStr().equals("过期")) {
                            showToast("该会员已过期");
                        }

                    }
                }
                if (type.equals(TYPE_SCORE_MEMBER)) {//积分调整
                    MemberHeadModel listBean = memberList.get(position);
                    if (listBean.is_click()) {
                        ScoreChangeActivity2.actionStart(mAc, listBean.getId());
                    } else {
                        if (listBean.getMl_status().equals("2")) {
                            showToast("该会员已锁定");
                        } else if (listBean.getMl_status().equals("3")) {
                            showToast("该会员已挂失");
                        } else if (listBean.getStatusStr().equals("过期")) {
                            showToast("该会员已过期");
                        }

                    }
                }
                if (type.equals(TYPE_MEMBER_LOCK)) {//会员锁定
                    LockActivity.actionStart(mAc, memberList.get(position));
                }
                if (type.equals(TYPE_CONSUME_MEMBER)) {//消费界面
                    MemberHeadModel memberHeadModel = memberList.get(position);
                    if ("2".equals(memberHeadModel.getMl_status())) {
                        showToast("会员已锁定");
                        return;
                    } else if ("3".equals(memberHeadModel.getMl_status())) {
                        showToast("会员已挂失");
                        return;
                    }
//                if (memberHeadModel.getStatus_info().is_valid()){
                    if (memberHeadModel.getStatusStr().equals("过期")) {
                        showToast("会员已过期");
                        return;
                    }
                    Intent intent = new Intent();
                    intent.putExtra(CommonMemberActivity.SEARCH_MEMBER_FLAG, true);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(CommonMemberActivity.SEARCH_MEMBER_MODEL, memberList.get(position));
                    intent.putExtra(CommonMemberActivity.SEARCH_MEMBER_BUNDLE, bundle);
                    setResult(CommonMemberActivity.RESULT_OK, intent);
                    finish();
                }
            }
        });

    }

    @OnClick({R.id.id_img_clean, R.id.id_img_qr_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_img_clean:
                idImgClean.setVisibility(View.GONE);
                idEdtSearch.setText("");
                keywords = "";
                memberList.clear();
                adapter.notifyDataSetChanged();
                hideSearch(false);
                break;
            case R.id.id_img_qr_code:
                getCameraManifest();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        // 如果是消费那里，返回键返回一组控数据，其他不用管
        if (type.equals(TYPE_CONSUME_MEMBER)) {
            Intent intent = new Intent();
            intent.putExtra(SEARCH_MEMBER_FLAG, false);
            Bundle bundle = new Bundle();
            bundle.putSerializable(SEARCH_MEMBER_MODEL, null);
            intent.putExtra(SEARCH_MEMBER_BUNDLE, bundle);
            setResult(RESULT_OK, intent);
        }
        finish();
    }


    public static final int SHOP_QR_CODE = 333;
    //获取照相机权限
    private static final int MY_PERMISSIONS_REQUEST_CAMERA = 1;

    private void getCameraManifest() {
        MPermissions.requestPermissions(this, MY_PERMISSIONS_REQUEST_CAMERA, Manifest.permission.CAMERA);
    }

    private void getCamera() {
        Intent intent = new Intent(mAc, CaptureActivity.class);
        startActivityForResult(intent, SHOP_QR_CODE);
    }

    @PermissionGrant(MY_PERMISSIONS_REQUEST_CAMERA)
    public void requestSuccess() {
        getCamera();
    }

    @PermissionDenied(MY_PERMISSIONS_REQUEST_CAMERA)
    public void requestError() {
        showToast("授权失败！");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 333:
                    //处理扫描结果（在界面上显示）
                    if (null != data) {
                        Bundle bundle = data.getExtras();
                        if (bundle == null) {
                            return;
                        }
                        String result = bundle.getString(CaptureActivity.RESULT);
                        idEdtSearch.setText(result);
                        inithttpsearch(idEdtSearch.getText().toString());
                    }
                    break;
            }
        }
    }

    class CommonMemberAdapter extends FragmentPagerAdapter {
        private List<MemberLevelModel.DataBean.ListBean> list;

        public CommonMemberAdapter(FragmentManager fm, List<MemberLevelModel.DataBean.ListBean> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position).getLevel_name();
        }

        //todo:k 新建step4
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new Fragment();
            if (type.equals(TYPE_JICI_MEMBER)) {
                fragment = CommonMemberFragmentJici.getInstentce(list.get(position).getId());
            } else if (type.equals(TYPE_MEMBER)) {
                fragment = CommonMemberFragmentMember.getInstentce(list.get(position).getId(), idTvAllMember);
            } else if (type.equals(TYPE_RECHARGE_MEMBER)) {
                fragment = CommonMemberFragmentRecharge.getInstentce(list.get(position).getId());
            } else if (type.equals(TYPE_RECHARGE_MEMBER_JICI)) {
                fragment = CommonMemberFragmentRechargeJici.getInstentce(list.get(position).getId());
            } else if (type.equals(TYPE_INTEGRAL_MEMBER)) {
                fragment = CommonMemberFragmentIntegral.getInstentce(list.get(position).getId());
            } else if (type.equals(TYPE_SCORE_MEMBER)) {
                fragment = CommonMemberFragmentScore.getInstentce(list.get(position).getId());
            } else if (type.equals(TYPE_MEMBER_LOCK)) {
                fragment = CommonMemberFragmentLock.getInstentce(list.get(position).getId());
            } else if (type.equals(TYPE_CONSUME_MEMBER)) {
                fragment = CommonMemberFragmentConsume.getInstentce(mAc, list.get(position).getId());
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return list.size();
        }

    }
}
